package org.rest
import grails.converters.JSON

class RootController {
	
	def show = {
		debugAccept()
		grailsApplication.controllerClasses.each {
			t -> println t.logicalPropertyName
			}
		response.setHeader("Content-Location",g.createLink(controller:"root",absolute:"true") as String)
		response.status = 200
		withFormat {
			all   {	render (contentType:"text/plain") { getJSONAsPlain()} }
			json  {	getJSON() }
						
		}                       
		
	}	
	
	def debugAccept = {
		def clientRequest = request.getHeader("accept")
		def serverResponse = request.format
		println "Client: ${clientRequest}\nServer: ${serverResponse}\n"    
	}
	
	def getJSONAsPlain = {
			render getRepresentation() as String
			
		}
	
	def getJSON = {
		render getRepresentation() as JSON
		
	}
	
	def getRepresentation = { suppliers ->
			def rootList = new ArrayList()
				grailsApplication.controllerClasses.each {t ->
				if (!(t.logicalPropertyName.equals("root"))){
					rootList.add(g.createLink(controller:"${t.logicalPropertyName}",absolute:"true"))
				}
		}
		return rootList 
	}
	
}